import { Component, Injector, OnInit } from '@angular/core';
import { ListBase } from 'app/modules/base/list-base';
import { MySelection } from 'app/modules/base/selection';

import {
  Task,
  TaskCategory,
  TaskListOption,
  TaskPeriod,
  TaskStatus,
  TaskService,
  TaskType,
} from '../../services/task.service';

export class ListComponent extends ListBase implements OnInit {
  public taskStatus = TaskStatus;
  public enumTaskPeriod = TaskPeriod;

  constructor(
    protected injector: Injector,
    protected category: TaskCategory,
    protected taskManager: TaskService,
  ) {
    super(injector);
  }

  supportBatch = true;
  selection = new MySelection<Task>('id');
  result$ = this.getData('list');
  taskStatusList = [TaskStatus.All, TaskStatus.Finish, TaskStatus.Cancel, TaskStatus.Failed];

  forwardOption(opt: TaskListOption) {
    this.router.navigate([], {
      relativeTo: this.route,
      queryParams: opt,
      queryParamsHandling: '',
    });
  }

  async updateTaskStatus(ids: Number[], status: TaskStatus) {
    for (let id of ids) {
      await this.taskManager.update({
        job_id: id,
        status: status,
      });
    }
    this.router.navigate([], {
      relativeTo: this.route,
      queryParams: { time: '' },
      queryParamsHandling: 'merge',
    });
  }

  pauseTask(ids: Number[]) {
    this.updateTaskStatus(ids, TaskStatus.Pause);
  }

  resumeTask(ids: Number[]) {
    this.updateTaskStatus(ids, TaskStatus.Running);
  }

  cancelTask(ids: Number[]) {
    this.updateTaskStatus(ids, TaskStatus.Cancel);
  }

  getSelectedIDList() {
    var ids = [];
    for (var task of this.selection.selected) {
      ids.push(task.jobs[0].id);
    }
    return ids;
  }
  batchPause() {
    console.log();
    this.pauseTask(this.getSelectedIDList());
  }

  batchResume() {
    this.resumeTask(this.getSelectedIDList());
  }

  batchCancel() {
    this.cancelTask(this.getSelectedIDList());
  }

  canCancel(job: any) {
    if (
      job.status != this.taskStatus.Cancel &&
      job.status != this.taskStatus.Failed &&
      job.status != this.taskStatus.Finish
    ) {
      return true;
    }
    return false;
  }

  ngOnInit() {}
}

@Component({
  selector: 'm-running-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss'],
})
export class RunningListComponent extends ListComponent {
  constructor(protected injector: Injector, protected taskManager: TaskService) {
    super(injector, 'running', taskManager);
    super.taskStatusList = [
      TaskStatus.All,
      TaskStatus.Running,
      TaskStatus.Pause,
      TaskStatus.Waiting,
    ];
  }
}
@Component({
  selector: 'm-history-list',
  templateUrl: './list.component.html',
  styleUrls: ['./list.component.scss'],
})
export class HistoryListComponent extends ListComponent {
  constructor(protected injector: Injector, protected taskManager: TaskService) {
    super(injector, 'history', taskManager);
    super.supportBatch = false;
    super.selection = null;
  }
}
@Component({
  selector: 'm-schedule-list',
  templateUrl: './schedule-list.component.html',
  styleUrls: ['./schedule-list.component.scss'],
})
export class ScheduleListComponent extends ListComponent {
  constructor(protected injector: Injector, protected taskManager: TaskService) {
    super(injector, 'schedule', taskManager);
    super.taskStatusList = [
      TaskStatus.All,
      TaskStatus.Running,
      TaskStatus.Pause,
      TaskStatus.Waiting,
    ];
  }
  taskPeriodList = [TaskPeriod.All, TaskPeriod.Schedule];
  taskTypeList = [
    TaskType.All,
    TaskType.Script,
    TaskType.SystemUpgrade,
    TaskType.SoftwareUpgrade,
    TaskType.Message,
  ];
}
